<template>
<div>
    <Modal title="Image" v-model="visible">
        <img :src="imageSrc" v-if="visible" style="width: 100%">
    </Modal>
     <Input placeholder="Enter something..." v-model="condition"><Button slot="append" icon="ios-search" @click="clickSearch"></Button></Input>
    <RadioGroup v-model="paging.status" type="button" @on-change="fRadio">
        <Radio label=-1>全部</Radio>
        <Radio label=0>已售出</Radio>
        <Radio label=1>正常出售</Radio>
        <Radio label=2>待审核</Radio>
        <Radio label=4>审核不通过</Radio>
        <Radio label=5>待送出</Radio>
        <Radio label=7>租赁中</Radio>
    </RadioGroup>
    <Button type="button" @click="activeList">不下架商品</Button>
    <Table border :columns="columns" :data="data"></Table>
    <Page :total="paging.total" @on-change="changepage" :page-size="paging.size"></Page>
</div>
</template>
<script>
import util from '../libs/util';
    export default {

        created() {
            this.initFormatter(); 
            this.paging = {
                page: 1,
                size: 8,
                status: -1,
            };
            this.load_data(this.paging);
            this.columns = [
                    {
                        title: '书籍ID',
                        key: 'id',
                    },
                     {
                        title: '用户ID',
                        key: 'uid',
                    },
                    {
                        title: '学校',
                        key: 'school',
                        render: function (h, params) {
                            var value = params.row.school;
                            if ( value == "") {
                                value = "尚未填写";
                            }
                            return h('div', 
                               value
                            );
                        }
                    },
                    {
                        title: '图书名称  ',
                        key: 'name',
                    },
                    {
                        title: '分类',
                        key: 'category_value',
                    },
                    {
                        title: 'ISBN',
                        key: 'ISBN',
                    }, 
                    {
                        title: '作者',
                        key: 'publisher',
                    },
                    {
                        title: '图片',
                        key: 'head',
                        render: (h, params) => {
                           var pic = params.row.head.split(";")[0];
                            return h("img",{
                                    attrs: {
                                        src: pic,
                                    },
                                    style:{
                                        width:"px",
                                        height:"45px",
                                    },
                                    on: {
                                        click: () => {
                                            this.handleView(pic)
                                        }
                                    }
                            })
                        }
                    },
                    {
                        title: '发布时间',
                        key: 'time',
                        render: function (h, params) {
                            return h('div', 
                               new Date(this.row.time*1000).Format('yyyy-MM-dd hh:mm')
                            );
                        }
                    },
                    {
                        title: '是否租赁',
                        key: 'is_rent',
                        render: function (h, params) {
                            var rent = '否';
                            if (params.row.is_rent == 1) {
                                rent = '是';
                            }
                            return h('div', 
                              rent
                            );
                        }
                    },
                     {
                        title: '所需堂豆',
                        key: 'bean',
                    },
                    {
                        title: '排序',
                        key: 'commodity_order',
                    },
                    {
                        title: '状态',
                        key: 'status',
                        render: function (h, params) {
                            var statu = ["售出", "正常出售", "待审核", "审核通过", "审核驳回", "待送出", "下架","租赁中"];
                            return h('div', 
                              statu[params.row.status]
                            );
                        }
                    },
                    {
                        title: '电话', 
                        key: 'phone',
                        render: function (h, params) {
                            var value = params.row.phone;
                            if ( value == "") {
                                value = "尚未填写";
                            }
                            return h('div', 
                               value
                            );
                        }
                    },
                     {
                        title: '活动商品状态',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            if (params.row.active == 0) {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.updateCommodity(params.index, 1)
                                            }
                                        }
                                    }, '正常'),
                                ]);
                            }  else {
                                return h('div',[
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.updateCommodity(params.index, 0)
                                            }
                                        }
                                    }, '不下架'),  ]  
                                )
                            } 
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            if (params.row.status == 2) {
                                return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.f(params.index, 4)
                                            }
                                        }
                                    }, '驳回'),
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.f(params.index, 1)
                                            }
                                        }
                                    }, '通过'),
                                ]);
                            }  else {
                                if (params.row.status == 4) {
                                    return h('div',
                                    [h('button',{
                                        style: {
                                            color: 'red',
                                            marginRight: '5px'
                                        },},'已审核'),
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '0px'
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.index, 1)
                                            }
                                        }
                                    }, '编辑'),
                                    ]
                                )
                                } else {
                                    return h('div',
                                    [h('button',{
                                        style: {
                                            color: 'red',
                                            marginRight: '5px'
                                        },},'已审核'),
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '0px'
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.index, 1)
                                            }
                                        }
                                    }, '编辑'),
                                     h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            marginRight: '0px',
                                        },
                                        on: {
                                            click: () => {
                                                this.f(params.index, 4)
                                            }
                                        }
                                    }, '立即驳回'),
                                    ]
                                )
                                }
                            } 
                            
                        }
                    }
                ];
          
        },
        data () {
            return {
                columns: [],
                condition: '',
                data: [],
                paging: {},
                visible: false,
                imageSrc: '',
                active: 0
            }
        },
        methods: {
            fRadio() {
                this.active = 1;
               this.paging.page = 1;
               this.condition = '';
               this.load_data(this.paging);
            },
            load_data(paging) {
                this.$http.post(util.baseURL+"commodity/getCollegeComodityList",paging,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data = response.data.data.list;
                              this.paging.total = response.data.data.total;
                            } else {
                               this.$Message.error("暂无商品列表！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            f (index, status) {
                var body = {
                    commodity_id : this.data[index].id,
                    status : status,
                    type : 3
                };
                //修改状态
                 this.$http.post(util.baseURL+"commodity/updataCommodityStatus",body,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data[index].status = status;
                              this.$Message.info("修改成功！");
                            } else {
                               this.$Message.error("修改失败！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            // 图片显示
            handleView(imageSrc) {
                this.imageSrc = imageSrc;
                this.visible = true;
            },
            // 编辑
            edit(index, status) {
               var commodity_id = this.data[index].id;
               this.$router.push({path:"/index/editcollegecommodity?t=3&commodity_id="+commodity_id});
            },
            changepage(page) {
               
                this.paging.page = page;
                 // 是否是不下架的下一页
                if (this.active == 1) {
                    return this.activeList();
                }
                if (this.condition.trim() == "") {
                    this.load_data(this.paging);
                } else {
                    this.search()
                }
            },
            clickSearch() {
                this.paging.page = 1;
                this.search();
            },
            // 搜索
            search() {
                var condition = this.condition.trim();
                if (condition == "") {
                    this.$Message.error("请输入正确的搜索条件！");
                } else {
                    var body = {
                        conditions: this.condition,
                        page : this.paging.page,
                        size : this.paging.size,
                        t : 3
                    };
                    console.log(body);
                    this.$http.post(util.baseURL+"commodity/searchCommodity",body,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data = response.data.data.list;
                              this.paging.total = response.data.data.total;
                            } else {
                               this.$Message.error("暂无商品列表！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
                }
            },
            // 修改商品不下架
            updateCommodity(index, active) {
                var body = {
                    commodity_id : this.data[index].id,
                    active : active,
                    t : 3
                };
                //修改状态
                 this.$http.post(util.baseURL+"commodity/updateCommodity",body,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data[index].active = active;
                            } else {
                               this.$Message.error("修改失败！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            // 不下架商品
            activeList() {
                this.active = 1
                this.paging.t = 3;
                this.$http.post(util.baseURL+"commodity/getActiveCommodity",this.paging,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                              this.data = response.data.data.list;
                              this.paging.total = response.data.data.total;
                            } else {
                               this.$Message.error("暂无商品列表！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },

            initFormatter(){
                Date.prototype.Format = function(fmt) { //
                    let o = {  
                        "M+" : this.getMonth()+1,                 //月份  
                        "d+" : this.getDate(),                    //日  
                        "h+" : this.getHours(),                   //小时  
                        "m+" : this.getMinutes(),                 //分  
                        "s+" : this.getSeconds(),                 //秒  
                        "q+" : Math.floor((this.getMonth()+3)/3), //季度  
                        "S"  : this.getMilliseconds()             //毫秒  
                    };  
                    if(/(y+)/.test(fmt))  
                        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
                    for(var k in o)  
                        if(new RegExp("("+ k +")").test(fmt))  
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
                    return fmt;  
                }
            }
        }
    }
</script>
